local Unit = _src("unit")

return Class{
   init = function (self, name)
      self.name = "Unit View"
   end,
   
   load = function (self)
      self:reset_gui ()
      local ul = self.gui:ul(20, 20, 200, 650)
      local utl = self.gui:ul(220, 20, 200, 650)
      utl.colors.normal = {bg = {0.4, 0.40, 0.50}, fg = {0.77, 0.91, 1}}
      utl.colors.hover  = {bg = {0.5, 0.5, 0.5}, fg = {1, 1, 1}}
      utl.onclick = function (index, b)
				self.view_unit = Unit(utl.items[index].title)
      end
      local ol = self.gui:ul(20, _lg.getHeight()-40, 200, 34, "Back")
      ol.onclick = function (i, b)
				Game:switch_state("mainmenu")
      end
			for _, filename in ipairs(_lf.getDirectoryItems("src/units")) do
				ul:add(string.match(filename, "(%a+).lua"))
			end
      ul:done()
      ul.onclick = function (index, b)
				if b == 1 then self:select_unit_type(index) end
      end
      self.ul = ul
      self.utl = utl
      self.ol = ol
   end,
   
   reset_gui = function (self)
      self.gui = _src("gui")()
   end,
   
   select_unit_type = function (self, index)
			self.utl.items = {}
			local name = self.ul.items[index].title
			local ut = Game:get_utype(name)
			lume(ut:list_units()):each(
				function (u)
					self.utl:add(u)
				end)
			self.utl:done()
			
   end,
   
   show_info = function (self)
   
   end,
   
   draw = function (self)
      self.gui:draw()
      if self.view_unit then
				_lg.print(self.view_unit:__to_string(), 350, 30)
      end
   end,
   
   mousepressed = function (self, x, y, b)
		self.ul:mousepressed (x, y, b)
		self.utl:mousepressed (x, y, b)
		self.ol:mousepressed (x, y, b)
   end,
   
   mousereleased = function (self, x, y, b)
		self.ul:mousereleased(x, y, b)
		self.utl:mousereleased(x, y, b)
		self.ol:mousereleased(x, y, b)
   end,
   
   mousemoved = function (self, x, y)
		self.ul:mousemoved(x, y)
		self.utl:mousemoved(x, y)
		self.ol:mousemoved(x, y)
   end,
   
   wheelmoved = function (self, x, y)
		self.ul:wheelmoved(x, y)
		self.utl:wheelmoved(x, y)
		self.ol:wheelmoved(x, y)
   end,
   
   resize = function (self, w, h)
      
   end,
   
   update = function (self, dt)
      self.gui:update(dt)
   end,
}
